package com.atbeijing.bigdata.spark.core.acc

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.{SparkConf, SparkContext}

object Spark05_Var_BC {

    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setMaster("local").setAppName("Acc")
        val sc = new SparkContext(conf)

        val rdd1 = sc.makeRDD(List(
            ("a", 1), ("b", 2), ("c", 3)
        ),2)

//        val rdd2 = sc.makeRDD(List(
//            ("a", 4), ("b", 5), ("c", 6)
//        ),2)
        val map = Map(("a", 4), ("b", 5), ("c", 6))

        val bc: Broadcast[Map[String, Int]] = sc.broadcast(map)

        // A => B
        // ("a", 1) => (a, (1,4))
        // ("b", 2) => (b, (2,5))
        // ("c", 3) => (c, (3,6))
        //val rdd3: RDD[(String, (Int, Int))] = rdd1.join(rdd2)
        // 3 Part => 1 Executor
        val rdd3 = rdd1.map {
            case ( k, v ) => {
                (k, ( v, bc.value.getOrElse(k, 0) ))
                bc.value.updated("e", 5)
            }
        }

        rdd3.collect().foreach(println)
        println(map)
        println(bc.value)

        sc.stop()

    }
}







































































































































































































































































































































































































































































































































































































































































































































































































































































































































































